package com.jwm.power.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import com.jwm.power.model.entity.InquiryT;

import com.jwm.power.model.entity.Users;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.service.InquiryService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/inquiry")
public class InquiryController {
    @Autowired
    private MapSearcher mapSearcher;
    @Resource
    private InquiryService inquiryService;
    @RequestMapping(value = "/queryInquiry", method = RequestMethod.GET)
    public void QueryInquiry(HttpServletRequest request, HttpServletResponse response){
        try{
            String page = request.getParameter("cPage");
            String size = request.getParameter("pSize");
            String searchName = request.getParameter("searchName");
            String queryBeginSeachDate =request.getParameter("queryBeginSeachDate");
            String queryEndSearchDate =request.getParameter("queryEndSearchDate");
            Users users = (Users) StpUtil.getSession().get("users");
            String deptcode_sale = "0103";
            String deptcode_cus = "0101";
            System.out.println(users.getName() + "," + users.getDeptcode());
            if(deptcode_cus.equals(users.getDeptcode())){
                Map<String, Object> params = MapUtils.builder()
                    .page(Integer.parseInt(page) - 1 , Integer.parseInt(size))                   // 偏移 0 条，查询 15 条（推荐写法）
                    .field("company", searchName).op("ct")
                    .field("inquiryDate", queryBeginSeachDate,queryEndSearchDate).op("bt")
                    .build();
                Map<String, Object> dataMap = new HashMap<>();
                SearchResult<Map<String, Object>> searchResult = mapSearcher.search(InquiryT.class, params);
                dataMap.put("data", searchResult.getDataList());
                dataMap.put("totals", searchResult.getTotalCount());
                Ut.toJson(response, dataMap);
            }
            // 如果是海外销售人员，则需要判断是哪位销售，进行筛选只有和登录人员相对应的
            if(deptcode_sale.equals(users.getDeptcode())) {
                Map<String, Object> params_sale = MapUtils.builder()
                     .page(Integer.parseInt(page) - 1 , Integer.parseInt(size))                   // 偏移 0 条，查询 15 条（推荐写法）
                     .field("sales",users.getName())
                     .field("company", searchName).op("ct")
                     .field("inquiryDate", queryBeginSeachDate,queryEndSearchDate).op("bt")
                     .build();
                Map<String, Object> dataMap_sale = new HashMap<>();
                SearchResult<Map<String, Object>> searchResult = mapSearcher.search(InquiryT.class, params_sale);
                dataMap_sale.put("data", searchResult.getDataList());
                dataMap_sale.put("totals", searchResult.getTotalCount());
                Ut.toJson(response, dataMap_sale);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "saveInquiry", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData SaveInquiry(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        return inquiryService.SaveInquiry(request);
    }
}
